package com.yjf.ljr.activity;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.yjf.ljr.R;
import com.yjf.ljr.connection.IjConnect;
import com.yjf.ljr.dataclass.InvestorsOpenAccountClass;
import com.yjf.ljr.dataclass.OpenInvestorClass;
import com.yjf.ljr.parser.RequestBuilder;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;

public class InvestorsOpenAccountActivity extends IjActivity implements OnClickListener {
	@ID("btokopenaccount")
	private Button mBtOkOpenAcount;
	@ID("etusername")
	private EditText mEtUsername;// 用户名
	@ID("realname")
	private EditText mEtRealname;// 真实姓名
	@ID("commandemail")
	private EditText mEtCommandemail;// 邮箱

	private TextView mTvReminder;
	private String uploadHost;
	private String token;
	private String availabelBroker;
	private String type;// 要开的账户类型

	// private String uploadHost;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_investors_open_account);
		initControl();

		mBtOkOpenAcount.setOnClickListener(this);
		// new InvestorsOpenAccountTask().execute();

	}

	private void initControl() {
		setLeftBtnClick();
		setTitleStr("投资人开户");
		mTvReminder = (TextView) findViewById(R.id.tvreminder);
		SpannableStringBuilder style = new SpannableStringBuilder(mTvReminder.getText().toString());
		style.setSpan(new ForegroundColorSpan(Color.RED), 0, 1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
		mTvReminder.setText(style);
	}

	/**
	 * 
	 * 投资人开户-获取前置信息
	 * 
	 * @author haiyang
	 * 
	 */
	class InvestorsOpenAccountTask extends AsyncTask<Void, Void, String> {

		private String mFailLog;
		private InvestorsOpenAccountClass data = new InvestorsOpenAccountClass();

		@Override
		protected void onPreExecute() {

			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... params) {
			RequestObject requestObject = new RequestObject();
			requestObject.method = "investorOpenAccount";
			return requestDataAndGetErrorMsg(requestObject, data);
		}

		@Override
		protected void onPostExecute(String result) {
			if ("-1".equals(data.code)) {
				loginWebview();
				return;
			}
			if (!TextUtils.isEmpty(result)) { // showToast(mFailLog);//fail
				showToast(result);
				dismissProgressDialog();
			} else {// success
				// showToast(data.msg);

				/*
				 * token = data.token; availabelBroker = data.availabelBroker;
				 * type = data.type; uploadHost = data.uploadHost;
				 */
				String tempRealName = "";

				try {
					tempRealName = URLEncoder.encode(mEtRealname.getText().toString(), "utf-8");
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				new OpenInvestorTask(mEtUsername.getText().toString(), tempRealName, mEtCommandemail.getText()
						.toString(), data.availabelBroker, data.uploadHost, data.token, data.type).execute();

			}
		}
	}

	/**
	 * 
	 * 投资人开户
	 * 
	 * @author haiyang
	 * 
	 */
	class OpenInvestorTask extends AsyncTask<Void, Void, String> {
		private String mFailLog;
		private String userName;
		private String realName;
		private String mail;
		private String availabelBroker;
		private String uploadHost;
		private String token;
		private String type;
		private OpenInvestorClass data = new OpenInvestorClass();

		public OpenInvestorTask(String userName, String realName, String mail, String availabelBroker,
				String uploadHost, String token, String type) {
			this.userName = userName;
			this.realName = realName;
			this.mail = mail;
			this.availabelBroker = availabelBroker;
			this.uploadHost = uploadHost;
			this.token = token;
			this.type = type;
		}

		@Override
		protected void onPreExecute() {

			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... params) {
			RequestObject requestObject = new RequestObject();
			requestObject.method = "appOpenInvestor";
			requestObject.map.put("userName", userName);
			requestObject.map.put("realName", realName);
			requestObject.map.put("mail", mail);
			// requestObject.map.put("availabelBroker", availabelBroker);
			// requestObject.map.put("uploadHost", uploadHost);
			requestObject.map.put("token", token);
			requestObject.map.put("type", type);

			return requestDataAndGetErrorMsg(requestObject, data);
		}

		@Override
		protected void onPostExecute(String result) {
			if ("-1".equals(data.code)) {
				loginWebview();
				return;
			}
			if (!TextUtils.isEmpty(result)) { // showToast(mFailLog); //fail
				showToast(result);
				dismissProgressDialog();
			} else {// success
				// showToast(data.msg);
				startActivity(new Intent(InvestorsOpenAccountActivity.this, InvestorsOpenAccountSuccessActivity.class));
				finish();
				dismissProgressDialog();
			}

		}
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.btokopenaccount:
			if (mEtUsername.getText().toString().length() < 6) {
				showToast("用户名的长度为6-20位字符");
				return;
			}
			if (TextUtils.isEmpty(mEtUsername.getText().toString())
					&& TextUtils.isEmpty(mEtRealname.getText().toString())
					&& TextUtils.isEmpty(mEtCommandemail.getText().toString())) {
				showToast("请输入用户名   真实姓名   常用邮箱");
				return;

			}
			if (TextUtils.isEmpty(mEtUsername.getText().toString())
					&& TextUtils.isEmpty(mEtRealname.getText().toString())) {
				showToast("请输入用户名   真实姓名   ");
				return;

			}
			if (TextUtils.isEmpty(mEtUsername.getText().toString())
					&& TextUtils.isEmpty(mEtCommandemail.getText().toString())) {
				showToast("请输入用户名    常用 邮箱");
				return;

			}
			if (TextUtils.isEmpty(mEtRealname.getText().toString())
					&& TextUtils.isEmpty(mEtCommandemail.getText().toString())) {
				showToast("请输入真实姓名   常用邮箱");
				return;

			}

			if (TextUtils.isEmpty(mEtUsername.getText().toString())) {
				showToast("请输入用户名  ");
				return;

			}
			if (TextUtils.isEmpty(mEtRealname.getText().toString())) {
				showToast("请输入真实姓名");
				return;

			}
			if (TextUtils.isEmpty(mEtCommandemail.getText().toString())) {
				showToast("请输入常用邮箱");
				return;

			} else {
				if (!isEmail(mEtCommandemail.getText().toString())) {
					showToast("常用邮箱格式不正确");
					return;
				}
			}
			new InvestorsOpenAccountTask().execute();
			showProgressDialog("正在确认开户，请稍等！");
			break;

		default:
			break;
		}
	}

	// 判断email格式是否正确
	public boolean isEmail(String email) {
		String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
		Pattern p = Pattern.compile(str);
		Matcher m = p.matcher(email);
		return m.matches();
	}
}